package com.ruicar.afs.cloud.basic.cms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.dispatch.contract.CmsInterfaceFeign;
import com.ruicar.afs.cloud.basic.common.entity.BasicCarDetails;
import com.ruicar.afs.cloud.basic.common.entity.BasicInsuranceInfo;
import com.ruicar.afs.cloud.basic.common.entity.BasicMainInfo;
import com.ruicar.afs.cloud.basic.common.service.*;
import com.ruicar.afs.cloud.common.config.ContractFeginConfig;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.contract.cms.vo.LoanProgressVo;
import com.ruicar.afs.cloud.common.modules.contract.cms.vo.LoanScheduleVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.ContractFeginParamEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.InsuranceTypeEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 多品合同对接聚合层贷款进度查询
 */
@Slf4j
@RestController
@RequestMapping("/icosLoanSchedule")
@AllArgsConstructor
public class IcosLoanScheduleController {

    private final BasicMainInfoService basicMainInfoService;
    private final BasicCarDetailsService basicCarDetailsService;
    private final BasicInsuranceInfoService basicInsuranceInfoService;
    private final CmsInterfaceFeign cmsInterfaceFeign;
    private final ContractFeginConfig contractFeginConfig;

    @ApiOperation(value = "贷款进度查询")
    @PostMapping("/getLoanSchedule")
    public IResponse<LoanScheduleVo> getLoanSchedule(@RequestParam("contractNo") String contractNo){
        log.info("------------------贷款进度查询入参为：{}------------------",contractNo);
        if (EmptyUtils.isEmpty(contractNo)){
            return IResponse.fail("合同编号不能为空");
        }
        LoanScheduleVo vo = new LoanScheduleVo();
        /**  基本信息 by ZC.GUO  **/
        BasicMainInfo mainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>query().lambda()
                .eq(BasicMainInfo::getContractNo,contractNo)
        );
        if (EmptyUtils.isEmpty(mainInfo)){
            log.info("------------------贷款进度查询未查到贷后数据，开始查询贷前数据------------------");
            //未找到数据说明是用申请编号查询的，去请求贷前
            //贷前数据
            Map<String, String> headers = new HashMap<>();
            headers.put(AfsEnumUtil.key(ContractFeginParamEnum.CASE_CLIENT_SECRET),contractFeginConfig.getCaseClientSecret());
            headers.put(AfsEnumUtil.key(ContractFeginParamEnum.CASE_CLIENT_ID),contractFeginConfig.getCaseClientId());
            headers.put(AfsEnumUtil.key(ContractFeginParamEnum.CONTENT_TYPE), MediaType.APPLICATION_JSON_VALUE);
            IResponse<LoanScheduleVo> response = cmsInterfaceFeign.getLoanSchedule(contractNo, headers);
            log.info("------------------贷款进度查询查询结果code:{},msg:{},data:{}------------------",response.getCode(),response.getMsg(),response.getData());
            if(CommonConstants.SUCCESS.equals(response.getCode())){
                vo = response.getData();
            }
        }else{
            //贷后数据
            vo.setContractNo(mainInfo.getContractNo());
            vo.setIntoFirstDate(mainInfo.getIntoFirstDate());
            vo.setApproveStatus("Converted");
            vo.setStartDate(mainInfo.getStartDate());
            vo.setApproveOperator("");
            vo.setApproveDate(mainInfo.getStartDate());
            vo.setContractStatus(mainInfo.getContractStatus().toString());
            vo.setActiveDate(mainInfo.getStartDate());
            vo.setActiveOperator(mainInfo.getCreateBy());
            vo.setArchiveDate(null);
            vo.setArchiveOperator("");
            vo.setSettleDate(mainInfo.getSettleDate());
            vo.setSettleOperator("");
            /**  车辆信息 by ZC.GUO  **/
            List<BasicCarDetails> carDetailsList = basicCarDetailsService.list(Wrappers.<BasicCarDetails>query().lambda()
                    .eq(BasicCarDetails::getContractNo,mainInfo.getContractNo())
            );
            if (EmptyUtils.isNotEmpty(carDetailsList)){
                BasicCarDetails carDetails = carDetailsList.get(0);
                vo.setCarMortDate(carDetails.getCarMortDate());
                vo.setCarMortOperator("");
                vo.setReleaseMortDate(carDetails.getReleaseMortDate());
                vo.setReleaseMortOperator("");
                vo.setReleaseMortDeliveryNo("");
            }
            /**  保险信息 by ZC.GUO  **/
            BasicInsuranceInfo insuranceInfo = basicInsuranceInfoService.getOne(Wrappers.<BasicInsuranceInfo>query().lambda()
                    .eq(BasicInsuranceInfo::getContractNo,mainInfo.getContractNo())
                    .eq(BasicInsuranceInfo::getInsuranceType, InsuranceTypeEnum.COMPULSORY)
            );
            if (EmptyUtils.isNotEmpty(insuranceInfo)){
                vo.setInsuranceApplyDate(null);
                vo.setInsuranceApplySendNo("");
                vo.setInsuranceOperator("");
                vo.setInsuranceEndDate(insuranceInfo.getInsuranceEndTime());
            }
        }
        log.info("------------------贷款进度查询查询贷后数据结果中的合同号为：{}------------------",vo.getContractNo());
        if(EmptyUtils.isNotEmpty(vo.getContractNo())){
            return IResponse.success(vo);
        }else{
            return IResponse.fail("未查询到任何数据");
        }

    }

}
